package cn.schoolwow.workflow.test.module.oa.loan.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.test.module.oa.common.flow.DeployConvertWorkFlowFlow;
import cn.schoolwow.workflow.test.module.oa.loan.flow.LoanAuditCommonFlow;
import cn.schoolwow.workflow.test.module.oa.loan.flow.LoanAuditFlow;
import com.alibaba.fastjson.JSONObject;

public class StartConvertLoanAuditFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        deployWorkFlow(flowContext);
        audit(flowContext);
    }

    @Override
    public String name() {
        return "转换原始数据-启动贷款审批流";
    }

    private void deployWorkFlow(FlowContext flowContext){
        String deployName = "贷款转换工作流";
        flowContext.putData("deployName", deployName);

        flowContext.startFlow(new DeployConvertWorkFlowFlow())
                .putCurrentCompositeFlowData("deployPath", "oa/贷款审批原始数据.json")
                .execute();
    }

    private void audit(FlowContext flowContext){
        {
            //未成年拒绝贷款
            JSONObject contextData = new JSONObject();
            contextData.put("field102", 10);
            flowContext.startFlow(new LoanAuditCommonFlow())
                    .putCurrentCompositeFlowData("contextData", contextData)
                    .putCurrentCompositeFlowInstanceData(WorkFlowInstanceStatus.Reject)
                    .execute();
        }
        {
            //信誉分过低拒绝贷款
            JSONObject contextData = new JSONObject();
            //年龄
            contextData.put("field102", 20);
            //信用分
            contextData.put("field103", 400);
            flowContext.startFlow(new LoanAuditCommonFlow())
                    .putCurrentCompositeFlowData("contextData", contextData)
                    .putCurrentCompositeFlowInstanceData(WorkFlowInstanceStatus.Reject)
                    .execute();
        }
        {
            //信誉分高自动通过贷款
            JSONObject contextData = new JSONObject();
            contextData.put("field102", 20);
            contextData.put("field103", 800);
            flowContext.startFlow(new LoanAuditCommonFlow())
                    .putCurrentCompositeFlowData("contextData", contextData)
                    .putCurrentCompositeFlowInstanceData(WorkFlowInstanceStatus.Finished)
                    .execute();
        }
        //人工审批
        flowContext.executeFlowList(new LoanAuditFlow());
    }

}
